package com.xiaoyao.novel.dao;

import java.io.File;
import java.io.IOException;
import java.io.Reader;
import java.util.Properties;

import org.apache.log4j.Logger;

import com.ibatis.common.resources.Resources;
import com.ibatis.sqlmap.client.SqlMapClient;
import com.ibatis.sqlmap.client.SqlMapClientBuilder;
import com.xiaoyao.novel.util.AutoConfigIP;
import com.xiaoyao.novel.util.FileUtil;

public class IbatisUtil {
	private static Logger logger = Logger.getLogger(IbatisUtil.class);
	static SqlMapClient client = null;
	static {
		try { 
			Reader reader = Resources.getResourceAsReader("SqlMapConfig.xml");
			String ip = AutoConfigIP.getLocalIP();
//			if (Constant.DEBUG) {
//				ip="";
//			}
			String propFile = ip + "jdbc.properties";  
			
			File file = new  File(FileUtil.getPath()+propFile);
			if (!file.exists()) {
				propFile =  "jdbc.properties";
			}
			
			logger.warn("加载JDBC配置:" + propFile);
			Properties props = Resources.getResourceAsProperties(propFile);
			client = SqlMapClientBuilder.buildSqlMapClient(reader, props);
		} catch (IOException e) {
			e.printStackTrace();
			throw new RuntimeException(e);
		}
	}

	public static SqlMapClient getSession() {
		return client;
	}

	public static void main(String[] args) {
		System.out.println("-");
		getSession();
	}

}
